package com.libinshen.wxpay.videoAliBaBa.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.libinshen.wxpay.common.constant.Constants;
import com.libinshen.wxpay.common.listener.ExcelListener;
import com.libinshen.wxpay.common.util.common.EasyPoiUtils;
import com.libinshen.wxpay.videoAliBaBa.entity.Teacher;
import com.libinshen.wxpay.videoAliBaBa.entity.vo.TeacherQueryVo;
import com.libinshen.wxpay.videoAliBaBa.entity.easyExcel.TeacherEasyExcel;
import com.libinshen.wxpay.videoAliBaBa.mapper.TeacherMapper;
import com.libinshen.wxpay.videoAliBaBa.service.TeacherService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author lbs
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Resource
    private TeacherMapper teacherMapper;

    @Override
    public IPage<Teacher> selectPage(Page<Teacher> pageParam, TeacherQueryVo teacherQueryVo) {

        //显示分页查询列表
//        1、排序：按照sort字段排序
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");

//        2、分页查询
        if (teacherQueryVo == null) {
            return baseMapper.selectPage(pageParam, queryWrapper);
        }

//        3、条件查询
        String name = teacherQueryVo.getName();
        Integer level = teacherQueryVo.getLevel();
        String joinDateBegin = teacherQueryVo.getJoinDateBegin();
        String joinDateEnd = teacherQueryVo.getJoinDateEnd();

        if (!StringUtils.isEmpty(name)) {
            queryWrapper.likeRight("name", name);
        }

        if (level != null) {
            queryWrapper.eq("level", level);
        }

        if (!StringUtils.isEmpty(joinDateBegin)) {
            queryWrapper.ge("join_date", joinDateBegin);
        }

        if (!StringUtils.isEmpty(joinDateEnd)) {
            queryWrapper.le("join_date", joinDateEnd);
        }

        return baseMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    public void importEasyPoi(MultipartFile file) {
        /***
         *  titleRows:
         *  headerRows:
         */
        List<Teacher> teachers = EasyPoiUtils.importExcel(file, 0, 1, Teacher.class);
        //调用本地mapper 方法批量上传数据到数据库，有个问题，如果是百万条数据，直接存放内存，如何处理
        //baseMapper.batchInsert();
    }

    @Override
    public void exportEasypoi(HttpServletResponse response) {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //如果导出百万级别的数据如何处理？？
        baseMapper.selectMaps(new QueryWrapper<>());
    }

    @Override
    public void importEasyExcel(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        String endFix = filename.substring(filename.indexOf(".") + 1);

        //导入03版本的excel
        if (Constants.ENDFIX_03.equals(endFix.toUpperCase())) {
            EasyExcel.read(file.getInputStream(), TeacherEasyExcel.class, new ExcelListener(teacherMapper))
                    .excelType(ExcelTypeEnum.XLS)
                    .sheet().doRead();
        }

        //导入07版本的
        if (Constants.ENDFIX_07.equals(endFix.toUpperCase())) {
            EasyExcel.read(file.getInputStream(), TeacherEasyExcel.class, new ExcelListener(teacherMapper))
                    .sheet().doRead();
        }
    }

    @Override
    public void exportEasyExcel(HttpServletResponse response) {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");

        //如果导出百万级别的数据如何处理？？
        try {
            String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");

            List<Teacher> teacherList = baseMapper.selectList(null);
            EasyExcel.write(fileName, Teacher.class).sheet("模板").doWrite(teacherList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
